package com.zhenyi.pxlm.controller.admin;

import javax.servlet.http.HttpServletRequest;

import com.jfinal.kit.JsonKit;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.DbPro;
import com.jfinal.plugin.activerecord.Page;
import com.zhenyi.core.VueUtils;
import com.zhenyi.core.ZIndexController;

import cn.hutool.core.util.PageUtil;

public class PXLMAdminController extends ZIndexController {
	protected DbPro db = Db.use("pxlm");

	/**
	 * 跳转过来的页面 referer
	 * 
	 * @Time 2018年11月24日 上午10:40:29
	 */
	protected void setReferer() {
		setAttr("referer", getRequest().getHeader("Referer"));
	}

	protected Integer getPageSize() {
		int pageSize = getParaToInt("pageSize", PAGE_SIZE);
		setAttr("pageSize", pageSize);
		return pageSize;
	}

	protected void setRainbow(Page<?> pages) {
		Integer requestPage = getParaToInt("page");

		HttpServletRequest request = getRequest();
		String url = request.getRequestURL().toString();
		String queryurl = request.getQueryString();
		if (StrKit.notBlank(queryurl)) {
			if (requestPage != null) {
				queryurl = queryurl.replace("&page=" + requestPage, "");
				queryurl = queryurl.replace("page=" + requestPage, "");
				if (StrKit.notBlank(queryurl)) {
					url = url + "?" + queryurl + "&page=";
				} else {
					url = url + "?page=";
				}
			} else {
				url = url + "?" + queryurl + "&page=";
			}
		} else {
			url = url + "?page=";
		}
		int pageTotal = pages.getTotalPage();
		int pageTotalRow = pages.getTotalRow();
		int page = pages.getPageNumber();
		int[] rainbow = PageUtil.rainbow(pages.getPageNumber(), pageTotal);
		setAttr("rainbow", rainbow);
		setAttr("pageUrl", url);
		setAttr("pageTotalRow", pageTotalRow);
		setAttr("pageSize", pages.getPageSize());
		setAttr("pageCurrent", page);
	}

	@Override
	public void setItems(Object items) {
		setAttr("items", JsonKit.toJson(items));
	}

	protected void setData(String name, Object info) {
		if (info == null) {
			return;
		}
		String json = VueUtils.getVueData(info);
		setAttr(name, json);
	}

	protected void setData(Object info) {
		setData("data", info);
		// setAttr("data", JsonKit.toJson(info));
		// setAttr("data", JSONUtil.toJsonStr(info));
		// String json = JsonUtils.toJson(info);
		// setAttr("data", json);
	}
}

